//
//  GenderController.m
//  Youhuiju
//
//  Created by liuovyue on 16/8/16.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "GenderController.h"
#import "CellGender.h"


@interface GenderController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)CellGender *cell;
@property (nonatomic, strong)NSIndexPath *indexPathAll;
@property (nonatomic, strong)UIButton *buttonOne;
@property (nonatomic, strong)UIButton *buttonTwo;
@property (nonatomic, strong)UIView *v;
@property (nonatomic, strong)UIButton *buttonHalf;
@end

@implementation GenderController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"buttonSelect" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.arr = @[@"男",@"女",@"保密"].mutableCopy;
    self.view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
   
    [self createNavigationBar];
    [self createTableView];
}

- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    
    
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"修改性别";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.textColor = [UIColor blackColor];
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.top.equalTo(self.viewBar.mas_top);
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
   
    
    
}
- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 84, [UIScreen mainScreen].bounds.size.width, 135*FitH) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.viewBar.mas_bottom).offset(20*FitH);
        make.right.equalTo(self.view.mas_right);
        make.left.equalTo(self.view.mas_left);
        make.height.mas_equalTo(135*FitH);
        
    }];
     [self.tableView registerNib:[UINib nibWithNibName:@"CellGender" bundle:nil] forCellReuseIdentifier:@"CellGender"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"CellGender"];
    [self.cell.buttonAdd addTarget:self action:@selector(genderAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.labelGender.text = [self.arr objectAtIndex:indexPath.row];
    

    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 45*FitH;
    
    
    
}
- (void)genderAction:(UIButton *)button {
    self.v = [button superview];
    UITableViewCell *cell = (CellGender *)[self.v superview];
    NSIndexPath *indexPathAll = [self.tableView indexPathForCell:cell];
    UILabel *lableGender = [[UILabel alloc]init];
    lableGender.text = [self.arr objectAtIndex:indexPathAll.row];
    [self.delegate passGender:lableGender];
//    NSLog(@"rrrrrr %@",lableGender.text);
    button.selected = !button.selected;
    button.tag = (indexPathAll.row + 1)*10;
    
    self.view.tag = 100;
     self.buttonOne = [self.view viewWithTag:10 ];
    self.buttonHalf = [self.view viewWithTag:20];
    self.buttonTwo = [self.view viewWithTag:30];
    
    if (indexPathAll.row == 1&&button.selected == 1){
        self.buttonTwo.selected = 0;
       
            
            self.buttonOne.selected = 0;
        
    }
  else  if (indexPathAll.row == 2&&button.selected == 1){
       
        self.buttonHalf.selected = 0;
      
        self.buttonOne.selected = 0;
        
        
  }else {
     
      self.buttonHalf.selected = 0;
      self.buttonTwo.selected = 0;
  }
    
}
//- (void)buttonSeletct{
//    
//    UIButton *buttonTwo = [self.view viewWithTag:2];
//    
//    NSLog(@"111111111  %d",buttonTwo.selected);
////    NSLog(@"nnnnnnnnnn  %d",button.selected);
////    if (self.indexPathAll.row == 2&&self.indexPathAll.row== 0) {
////        
////        button.selected = 0 ;
////        
////    }
//    
//    
//    
//    
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.cell indexPath:indexPath];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
